/**
 * <p>Title: MaterialBatchPropertyDaoImpl</p>
 * <p>Description: The Implements of MaterialBatchPropertyDao</p>
 * <p>Copyright: Shanghai Batchsight Pharmaceutical Technologies, Inc. Copyright(c) 2016</p>
 * @author Wan Shuaishuai
 * @version 1.0
 * <pre>Histroy:
 *       2016-11-23  Wan Shuaishuai  Create
 *</pre>
*/
package cn.jhg.account.persist;

import javax.annotation.Resource;
import org.bson.Document;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import cn.jhg.account.entity.MaterialBatch;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

@Repository
public class MaterialBatchPropertyDaoImpl implements MaterialBatchPropertyDao {
	@Resource
	private MongoTemplate mongoTemplate;
	Logger logger=LoggerFactory.getLogger(MaterialBatchPropertyDaoImpl.class);
	
	/**
	 * <p> Function: addBatchProperty </p>
	 * <p> Description: 新增原料批次</p>
	 * @param batch	批次实体
	 * @return	int  ErrorCode
	 */
	@Override
	public int addBatchProperty(MaterialBatch batch) {
		try{
			DBObject object = new BasicDBObject();//设置需要插入到数据库的文档对象
			ObjectId propId = new ObjectId(batch.getPropId());
			object.put("_id", propId);
			object.put("property", batch.getProperty());
			mongoTemplate.insert(object,"material_batch_property");
			logger.info("[INF223196780] 添加物料批次属性成功");
			return 0;//元数据添加成功
		}catch(Exception e){
			logger.error("[ERR223196782] 异常:添加物料批次属性失败",e.getMessage());
			return 223196782;//异常错误代码
		}
	}
	
	/**
	 * <p> Function: updateBatchProperty </p>
	 * <p> Description: 更新原料批次</p>
	 * @param batch	批次实体
	 * @return	int  ErrorCode
	 */
	@Override
	public int updateBatchProperty(MaterialBatch batch) {
		try {
			if(batch.getPropId()==null){
				logger.error("[ERR223196798] 属性id为空,更新失败");
				return 223196798; //属性id为空,更新失败
			}
			Query query = new Query();
			query.addCriteria(Criteria.where("_id").is(batch.getPropId()));
			Update update = new Update();
			update.set("property",batch.getProperty());
			mongoTemplate.updateMulti(query, update, Document.class, "material_batch_property");
			logger.info("[INF223196902] 更新物料批次属性成功");
			return 0;
		} catch (Exception e) {
			logger.error("[ERR223196919] 异常:更新物料批次属性失败",e.getMessage());
			return 223196919;//更新失败
		}
	}
	
	/**
	 * <p> Function: deleteBatchProperty </p>
	 * <p> Description: 删除原料批次</p>
	 * @param id	属性id
	 * @return	int  ErrorCode
	 */
	@Override
	public int deleteBatchProperty(ObjectId id) {
		try {
			Document batchProperty = mongoTemplate.findById(id, Document.class, "material_batch_property");
			if(batchProperty==null){
				logger.error("[ERR223196927] 属性id不存在，删除原料批次失败");
				return 223196927;
			}
			Query query = new Query();
			query.addCriteria(Criteria.where("_id").is(id));
			mongoTemplate.remove(query, MaterialBatch.class, "material_batch_property");
			logger.info("[INF223196939] 删除原料批次成功");
			return 0;
		} catch (Exception e) {
			logger.error("[ERR223196947] 异常:删除原料批次失败",e.getMessage());
			return 223196947;//删除失败
		}
	}
	
	/**
	 * <p> Function: queryBatchPropertyById </p>
	 * <p> Description:  </p>
	 * @param id	属性id
	 * @return	MaterialBatchProperty  
	 */
	@Override
	public Document queryBatchPropertyById(ObjectId id) {
		Document doc = new Document();
		Document batch = new Document();
		try {
			batch = mongoTemplate.findById(id, Document.class, "material_batch_property");
			if(batch==null){
				logger.error("[ERR223197059] 属性id不存在，查询原料批次失败");
				doc.append("errorCode", 223197059);
			}else{
				logger.info("[INF223196960] 通过id查询物料批次属性成功");
				doc.append("batch", batch);
				doc.append("errorCode", 0);
			}
			return doc;
		} catch (Exception e) {
			logger.error("[ERR223197051] 异常:通过id查询物料批次属性失败",e.getMessage());
			doc.append("errorcode", 223197051);
			return doc; //通过id查询物料批次属性失败
		}
	}

}
